<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>封装集合</title>
</head>
<body>
<script>
    /*
                add(value)
                remove(value)
                has(value)
                clear()
                size()
                values()

    * */
    //封装集合
    function Set() {
        //属性
        this.items = {};
        //方法
        //add
        Set.prototype.add = function (value) {
            //判断元素是否重复
            if(this.has(value)){
                return false;
            }
            //添加
            this.items[value] = value;
            return true;
        }

        //remove
        Set.prototype.remove = function (value) {
            //判断是否存在
            if (!this.has(value)){
                return false;
            }
            //删除数据
            delete this.items[value];
            return true;
        }

        //has
        Set.prototype.has = function (value) {
            return  this.items.hasOwnProperty(value);
        }

        //clear
        Set.prototype.clear = function () {
            this.items = {};
        }

        //size
        Set.prototype.size = function () {
            return Object.keys(this.items).length;
        }

        //values
        Set.prototype.values = function () {
            return Object.keys(this.items);
        }

        //并集
        Set.prototype.union = function (otherSet) {
            //1.创建一个空集合
            var newSet = new Set();
            var values = this.values();
            for (let i = 0; i < values.length; i++) {
                newSet.add(values[i]);
            }
            values = otherSet.values();
            for (let i = 0; i < values.length; i++) {
                newSet.add(values[i]);
            }
            return newSet;
        }

        //交集
        Set.prototype.intersection = function (otherSet) {
            //1.创建一个空集合
            var newSet = new Set();
            //2.创建一个变量来接收A集合的元素
            var values = this.values();
            for (let i = 0; i < values.length; i++) {
                if (otherSet.has(values[i])){
                    newSet.add(values[i]);
                }
            }
            return newSet;
        }
        //差集
        Set.prototype.differenceSet = function (otherSet) {
            //1.创建一个空集合
            var newSet = new Set();
            //2.
            var values = this.values();
            for (let i = 0; i < values.length; i++) {
                if (!otherSet.has(values[i])){
                    newSet.add(values[i]);
                }
            }
            return newSet;
        }

        //子集
        Set.prototype.subset = function (otherSet) {
            //获取集合A的所有元素
            var values = this.values();
            for (let i = 0; i < values.length; i++) {
                if (!otherSet.has(values[i])){
                    return false;
                }
            }
            return true;
        }
    }
    //测试
    var set = new Set();
    //添加
    set.add('aaa');
    set.add('aaa');
    set.add('bbb');
    set.add('ccc');
    set.remove('abc');
    alert(set.values());
    alert(set.size());
    set.clear();
    alert(set.size());
    var set1 = new Set();
    set1.add('aaa');
    set1.add('bbb');
    set1.add('ddd');
    set1.add('eee');
    //set aaa,bbb,ccc
    //set1 aaa,bbb,ddd,eee
    alert(set.union(set1).values());  //aaa,bbb,ccc,ddd,eee
    alert(set.intersection(set1).values()); //aaa,bbb
    alert(set.differenceSet(set1).values()); //ccc
    alert(set.subset(set1)); //false

</script>
</body>
</html>